<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>fuint会员营销管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Expires" content="0">

    <link rel="stylesheet" href="layui/css/layui.css">
    <style>
        /* 移动端 */
        @media screen and (max-width: 768px) {
            .layui-layout-admin .layui-layout-left,
            .layui-layout-admin .layui-body,
            .layui-layout-admin .layui-footer{left: 0;}
            .layui-layout-admin .layui-side{left: -300px;}
        }
        .shop-bread{
            visibility:visible;
        }
        .shop-box{
            display: flex;
        }
        .necessary{
            color: red;
        }
        .shop-radio {
            display: flex;
            align-content: center;
        }
        .shop-search{
            display: flex
        }
    </style>
</head>
<body>

        <!-- 内容主体区域 -->
        <div >
            <!-- 内容主体区域  面包屑-->
            <span class="layui-breadcrumb" lay-separator="/" >
              <a href="index.html">首页 </a>
             <a><cite>店铺管理  </cite></a>
              <a href="shoplist.html">店铺列表  </a>
            </span>
            <!-- 水平线 -->
            <hr class="layui-bg-blue">
            <!-- 搜索条件 -->
            <fieldset class="layui-elem-field">
                <legend>筛选条件</legend>

                <div class="layui-field-box shop-box">

                    <form class="layui-form shop-search">
                        <label class="layui-form-label">店铺名称</label>
                        <div class="layui-input-inline">
                            <input type="text" id="search_shopname" placeholder="请输入店铺名称" class="layui-input">
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-block">
                                <select name="interest" lay-filter="aihao" id="search_status">
                                    <option value="1">在营</option>
                                    <option value="0">不在营</option>
                                </select>
                            </div>
                        </div>
                    </form>

                    <div class="layui-form-item layui-form-pane">
                        <div class="layui-inline">
                            <button class="layui-btn" id="searchVal">搜索</button>
                            <button class="layui-btn layui-btn-primary layui-border-orange" id="iReset">重置</button>
                        </div>
                    </div>
                </div>
            </fieldset>
    <table class="layui-hide" id="test" lay-filter="test"></table>

            <script type="text/html" id="toolbarDemo">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="insertShop">新增店铺</button>
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                    <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                    <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
                </div>
            </script>

            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>

            <script type="text/html" id="insert_form">
                <div class="layui-field-box">
                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label"><text class="necessary">*</text>店铺名称</label>
                        <div class="layui-input-inline">
                            <input type="text" id="insert_shopname" class="layui-input" placeholder="请输入店铺名" lay-verify="required">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label"><text class="necessary">*</text>联系人姓名</label>
                        <div class="layui-input-inline">
                            <input type="text" id="insert_contact" class="layui-input" placeholder="请输入联系人姓名" lay-verify="required">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label"><text class="necessary">*</text>联系人电话</label>
                        <div class="layui-input-inline">
                            <input type="text" id="insert_contact_phone" class="layui-input" placeholder="请输入联系人电话" lay-verify="required">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">店铺地址</label>
                        <div class="layui-input-block">
                            <input type="text" id="insert_address" class="layui-input" placeholder="请输入店铺地址" style="width: 80%;">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">营业时间</label>
                        <div class="layui-input-block">
                            <select name="time" id="time">
                                <option value=""></option>
                                <option value="5:00-12:00">5:00-12:00</option>
                                <option value="14:00-18:00">14:00-18:00</option>
                                <option value="9:00-22:00">9:00-22:00</option>
                                <option value="24小时营业">24小时营业</option>
                            </select>
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">是否默认</label>
                        <div class="layui-input-block">
                            <input type="checkbox" name="insert_State" lay-skin="switch" lay-text="开启|关闭" value="1">
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-pane">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block shop-radio" >
                            <input type="radio" name="insert_status" value="1" title="在营" checked>
                            <text>在营</text>
                            <input type="radio" name="insert_status" value="0" title="不在营" >
                            <text>不在营</text>
                        </div>
                    </div>
                </div>
            </script>

        </div>

<script src="layui/layui.js" ></script>
<script src="jquery/jquery-3.6.0.js" ></script>
<script>
  //  let username = sessionStorage.getItem("username");

  //  $("#header_username").text(username);
// 重置
    $("#iReset").click(function () {
        $("#search_shopname").val("");
        tableFlush();
    });
// 搜索
    $("#searchVal").click(function () {
        let search_shopname = $("#search_shopname").val();
        let search_status = $("#search_status option:selected").val();
        let whereJSON = {};
        if(search_shopname != null && search_shopname != '') {
            whereJSON.shopName = search_shopname;
        }
        if(search_status != null && search_status != '') {
            whereJSON.shopStatus = search_status;
        }
        console.log(whereJSON)
        tableFlush(whereJSON);
    });

    function tableFlush(whereJSON) {
        layui.use(['table','form','element'], function(){
            var table = layui.table;
            var form = layui.form;
            //  render  渲染
            table.render({
                elem: '#test'
                ,url:'/shop/selectByPage'
                ,where:whereJSON
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    ,layEvent: 'LAYTABLE_TIPS'
                    ,icon: 'layui-icon-tips'
                }]
                ,title: '店铺数据表'
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{title:'ID', width:50, unresize: true, templet: function(res){
                            return '<em>'+ res.shopId +'</em>'
                        }}
                    ,{title:'是否默认', width:90, unresize: true, templet: function (res) {
                            let menuId = res.shopId;
                            if (res.defaultState == 1) {
                                return "   <input type='checkbox'  menuId = '" + menuId + "' id='defaultState' lay-filter='defaultState' lay-event='defaultState' lay-skin='switch' lay-text='ON|OFF' checked>"
                            } else if (res.defaultState == 0) {
                                return "   <input type='checkbox'  menuId = '" + menuId + "'  id='defaultState' lay-filter='defaultState' lay-event='defaultState' lay-skin='switch' lay-text='ON|OFF'>"
                            }
                        }
                    }
                    ,{title:'店铺名称', width:120, unresize: true, templet: function(res){
                            return '<em>'+ res.shopName +'</em>'
                        }}
                    ,{title:'联系人', width:100, unresize: true, templet: function(res){
                            return '<em>'+ res.contacts +'</em>'
                        }}
                    ,{title:'联系人电话', width:120, unresize: true, templet: function(res){
                            return '<em>'+ res.contactsPhone +'</em>'
                        }}
                    ,{title:'地址', width:200, unresize: false, templet: function(res){
                            return '<em>'+ res.address +'</em>'
                        }}
                    ,{title:'创建时间', width:180, unresize: true, templet: function(res){
                            return '<em>'+ res.shopCreateTime +'</em>'
                        }}
                    ,{title:'营业时间', width:120, unresize: true, templet: function(res){
                            return '<em>'+ res.businessHours +'</em>'
                        }}
                    ,{title:'状态', width:90, unresize: true, templet: function (res) {
                            let menuId = res.shopId;
                            if (res.shopStatus == 1) {
                                return "   <input type='checkbox'  menuId = '" + menuId + "' lay-filter='shopStatus' lay-skin='switch' lay-text='ON|OFF' checked>"
                            } else if (res.shopStatus == 0) {
                                return "   <input type='checkbox'  menuId = '" + menuId + "'  lay-filter='shopStatus' lay-skin='switch' lay-text='ON|OFF'>"
                            }
                        }}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
                ]]
                ,page: true
            });

            //头工具栏事件
            table.on('toolbar(test)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'insertShop':
                        layer.open({     // 弹出框处理  模态框
                            type: 1               // 模态框
                            ,title: '新增店铺' //不显示标题栏
                            ,closeBtn: false
                            ,area: ['500px','500px']  // 宽和高
                            ,shade: 0.5
                            ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                            ,btn: ['新增', '取消']
                            ,btnAlign: 'c'
                            ,moveType: 1 //拖拽模式，0或者1
                            ,content: $('#insert_form').html()         // 引入自定义表单
                            ,btn1: function(index, layero){            // 这个是新增按钮点击事件
                                // layer.msg("新增按钮执行");
                                let insert_shopname = $("#insert_shopname").val();
                                let insert_contact = $("#insert_contact").val();
                                let insert_contact_phone = $("#insert_contact_phone").val();
                                let insert_address = $("#insert_address").val();
                                let insert_status = $("input[name='insert_status']:checked").val();
                                let insert_time = $("#time option:selected").val();
                                let insert_State = $("input[name='insert_State']:checked").val();

                                $.ajax({
                                    url:"/shop/insertShop",           // 请求路径
                                    type:"post",                  // 请求的方式，不区分大小写
                                    cache:false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                                    contentType:"application/json;charset=utf-8",
                                    data: JSON.stringify({
                                        shopName:insert_shopname,
                                        contacts:insert_contact,
                                        contactsPhone:insert_contact_phone,
                                        address:insert_address,
                                        businessHours:insert_time,
                                        defaultState:insert_State,
                                        shopStatus:insert_status,
                                    }),
                                    datatype:"json",        // 返回类型，text文本、html页面、json数据
                                    success:function(response){
                                        console.log("返回: " + response);
                                        if(response.code === 200) {
                                            layer.msg("新增店铺成功!");
                                            layer.close(index);                         // 关闭当前窗口
                                            tableFlush();
                                        }
                                    },
                                    error:function(response){
                                        console.log("出错返回: " + response);
                                    }
                                });
                            }
                            ,success: function(layero){

                            }
                        });
                        break;
                    case 'getCheckData':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        break;
                    case 'getCheckLength':
                        var data = checkStatus.data;
                        layer.msg('选中了：'+ data.length + ' 个');
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选': '未全选');
                        break;

                    //自定义头工具栏右侧图标 - 提示
                    case 'LAYTABLE_TIPS':
                        layer.alert('这是工具栏右侧自定义的一个图标按钮');
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(test)', function(obj){
                var data = obj.data;
                console.log(obj.event)
                if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        $.ajax({
                            url:"/shop/deleteById",           // 请求路径
                            type:"post",                  // 请求的方式，不区分大小写
                            cache:false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                            data: {
                                shopId:data.shopId
                            },
                            datatype:"json",        // 返回类型，text文本、html页面、json数据
                            success:function(response){
                                console.log("返回: " + response);
                                if(response.code === 200) {
                                    layer.msg("删除店铺成功!");
                                    layer.close(index);                         // 关闭当前窗口
                                    tableFlush();
                                }
                            },
                            error:function(response){
                                console.log("出错返回: " + response);
                            }
                        });
                    });
                } else if(obj.event === 'edit'){
                    layer.open({     // 弹出框处理  模态框
                        type: 1               // 模态框
                        ,title: '信息' //不显示标题栏
                        ,closeBtn: false
                        ,area: ['500px','500px']  // 宽和高
                        ,shade: 0.5
                        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
                        ,btn: ['确定', '取消']
                        ,btnAlign: 'c'
                        ,moveType: 1 //拖拽模式，0或者1
                        ,content: $('#insert_form').html()         // 引入自定义表单
                        ,btn1: function(index, layero){            // 这个是新增按钮点击事件
                            // layer.msg("新增按钮执行");
                            // $("#insert_shopname").val(data.shopName);
                            // $("#insert_contact").val(data.shopName);
                            // $("#insert_contact_phone").val(data.shopName);
                            // $("#insert_address").val(data.shopName);

                            let shopId = data.shopId
                            let insert_shopname = $("#insert_shopname").val();
                            let insert_contact = $("#insert_contact").val();
                            let insert_contact_phone = $("#insert_contact_phone").val();
                            let insert_address = $("#insert_address").val();
                            let insert_status = $("input[name='insert_status']:checked").val();
                            let insert_time = $("#time option:selected").val();
                            let insert_State = $("input[name='insert_State']:checked").val();

                            $.ajax({
                                url:"/shop/updateById",           // 请求路径
                                type:"post",                  // 请求的方式，不区分大小写
                                cache:false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                                contentType:"application/json;charset=utf-8",
                                data: JSON.stringify({
                                    shopId:shopId,
                                    shopName:insert_shopname,
                                    contacts:insert_contact,
                                    contactsPhone:insert_contact_phone,
                                    address:insert_address,
                                    businessHours:insert_time,
                                    // defaultState:insert_State,
                                    shopStatus:insert_status,
                                }),
                                datatype:"json",        // 返回类型，text文本、html页面、json数据
                                success:function(response){
                                    console.log("返回: " + response);
                                    if(response.code === 200) {
                                        layer.msg("修改信息成功!");
                                        layer.close(index);                         // 关闭当前窗口
                                        tableFlush();
                                    }
                                },
                                error:function(response){
                                    console.log("出错返回: " + response);
                                }
                            });
                        }
                        ,success: function(layero){

                        }
                    });
                }
            });
            //改变是否默认店铺
            form.on('switch(defaultState)', function (data) {
                // console.log(data.elem.checked); // 开关是否开启，true或者false
                // console.log(data.value);        // 开关value值，也可以通过data.elem.value得到
                let shopId = data.elem.attributes['menuId'].nodeValue
                let defaultState = data.elem.checked?"1":"0"
                $.ajax({
                    url:"/shop/updateById",           // 请求路径
                    type:"post",                  // 请求的方式，不区分大小写
                    cache:false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                    contentType:"application/json;charset=utf-8",
                    data: JSON.stringify({
                        shopId:shopId,
                        defaultState:defaultState
                    }),
                    datatype:"json",        // 返回类型，text文本、html页面、json数据
                    success:function(response){
                        console.log("返回: " + response);
                        if(response.code === 200) {
                            layer.msg("修改信息成功!");
                            // layer.close(index);                         // 关闭当前窗口
                        }
                    },
                    error:function(response){
                        console.log("出错返回: " + response);
                    }
                });
            });
            //改变状态
            form.on('switch(shopStatus)', function (data) {
                // console.log(data.elem.checked); // 开关是否开启，true或者false
                // console.log(data.value);        // 开关value值，也可以通过data.elem.value得到
                let shopId = data.elem.attributes['menuId'].nodeValue
                let shopStatus = data.elem.checked?"1":"0"
                $.ajax({
                    url:"/shop/updateById",           // 请求路径
                    type:"post",                  // 请求的方式，不区分大小写
                    cache:false,                  // 关闭缓存，目的是为了避免部分浏览器缓存加载出错(IE)
                    contentType:"application/json;charset=utf-8",
                    data: JSON.stringify({
                        shopId:shopId,
                        shopStatus:shopStatus
                    }),
                    datatype:"json",        // 返回类型，text文本、html页面、json数据
                    success:function(response){
                        console.log("返回: " + response);
                        if(response.code === 200) {
                            layer.msg("修改信息成功!");
                            // layer.close(index);                         // 关闭当前窗口
                        }
                    },
                    error:function(response){
                        console.log("出错返回: " + response);
                    }
                });
            });
        });
    }


    tableFlush();
</script>
        <style>
            .layui-breadcrumb a {
                color: black !important;
            }
            .layui-breadcrumb{
                padding: 10px;
            }
        </style>
</body>
</html>
